/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.swing.table;

import java.util.Map;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

/**
 * Undo/Redo support for table modifications.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class UndoableTableModelEdit extends AbstractUndoableEdit {

    private static final long serialVersionUID = -1568455574731768310L;
    private EnhancedTableModel tableModel = null;
    private int rowAffected = Integer.MIN_VALUE;
    private Map<String, Object> rowData = null;
    private boolean isAdd = false;

    /**
     * Constructs a standard table model edit.
     * <p>
     * This construct is suited for table model deletions by default.
     * 
     * @param affected the row that has been affected by the edit.
     * @param data row mapping of column names to objects.
     * @param model model that was modified as a result of the edit.
     * @see #UndoableTableModelEdit(int, Map, EnhancedTableModel, boolean)
     */
    public UndoableTableModelEdit(int affected, Map<String, Object> data, EnhancedTableModel model) {

        this(affected, data, model, false);
    }

    /**
     * Constructs a standard table model edit.
     * <p>
     * 
     * @param affected the row that has been affected by the edit.
     * @param data row mapping of column names to objects.
     * @param model model that was modified as a result of the edit.
     * @param doAdd flag indicating this edit is an addition operation.
     */
    public UndoableTableModelEdit(int affected, Map<String, Object> data, EnhancedTableModel model, boolean doAdd) {

        rowAffected = affected;
        rowData = data;
        tableModel = model;
        isAdd = doAdd;
    }

    @Override
    public void redo() throws CannotRedoException {

        super.redo();
        if (isAdd) {
            tableModel.insertRow(rowAffected, rowData);
        } else {
            tableModel.removeRow(rowAffected);
        }
    }

    @Override
    public void undo() throws CannotUndoException {

        super.undo();
        if (isAdd) {
            tableModel.removeRow(rowAffected);
        } else {
            tableModel.insertRow(rowAffected, rowData);
        }
    }
}
